﻿namespace SenenFernandez.Sensktop.Configuration
{
    using SenenFernandez.SmartExtensionMethods;

    using System.Windows;

    public class WorkingArea
    {
        private static readonly object Sync = new object();
        private static WorkingArea instance;
        private Display display;

        public static WorkingArea Instance
        {
            get
            {
                lock (Sync)
                {
                    return instance ?? (instance = new WorkingArea());
                }
            }
        }

        public Display Display
        {
            get
            {
                SetDisplay(false);
                return display;
            }
        }

        public double Left
        {
            get { return Display != null ? Display.Bounds.Left : 0; }
        }

        public double Top
        {
            get { return Display != null ? Display.Bounds.Top : 0; }
        }

        public double Width
        {
            get
            {
                return Display != null
                    ? Display.Bounds.Width
                    : DefaultValue.DisplayResolutionWidth;
            }
        }

        public double Height
        {
            get
            {
                return Display != null
                    ? Display.Bounds.Height
                    : DefaultValue.DisplayResolutionHeight;
            }
        }

        public double DesktopTop
        {
            get { return Height - DesktopHeight; }
        }

        public double DesktopHeight
        {
            get { return SensktopConfiguration.Instance.Height; }
        }

        public Rect Area
        {
            get { return new Rect(Left, Top, Width, DesktopTop); }
        }

        public double AvailableWidth
        {
            get { return Area.Width; }
        }

        public double AvailableHeight
        {
            get { return Area.Height; }
        }

        public void SetDisplay(bool isDisplaySettingsChanged)
        {
            if (display == null || isDisplaySettingsChanged)
            {
                display = SmartScreenManager.GetMonitorOrDefault(SensktopConfiguration.Instance.DisplayName);
            }
        }
    }
}